<template>
  <center>
    <h2>新增步骤单元</h2>
  </center>
  <center>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" style="width: 500px">
    <el-form-item label="步骤编号" prop="stepno">
      <el-input v-model="ruleForm.stepno" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="单元编号" prop="unitno">
      <el-select v-model="ruleForm.unitno" placeholder="请选择单元编号" @change="unitnamechange" style="width: 350px">
        <el-option
            v-for="item in ruleForm.unitnoList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="单元名称" prop="unitname">
      <el-input v-model="ruleForm.unitname" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="单元优先等级" prop="priority">
      <el-input v-model="ruleForm.priority" onkeyup="value=value.replace(/[^\d]/g,'')"></el-input>
    </el-form-item>
  </el-form>
  </center>

  <center>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('ruleForm')">新建步骤单元</el-button>
      <el-button type="danger" @click="onCancel()">完成</el-button>
    </el-form-item>
  </center>
</template>

<script>
import {get} from "@/utils/http";
import {ElMessage} from "element-plus";

export default {
  name: "AddUnit",
  data() {
    return {
      ruleForm: {
        stepno: '',
        unitno: '',
        unitname: '',
        priority: '',
        unitnoList: []
      },
      rules: {
        stepno:[
          { required: true, message: '请输入步骤编号', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        unitno:[
          { required: true, message: '请输入单元编号', trigger: 'blur' }
        ],
        priority:[
          { required: true, message: '请输入单元优先等级', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  created(){
    this.ruleForm.stepno = this.$route.params.stepno;
    this.getOptions();
  },
  methods: {
    unitnamechange(){
      let that = this;
      get('/getaddunitnojs', {
      }).then(function (res) {
        if (res.code === 1) {
          for(var i=0;i<res.data.length;i++){
            if(res.data[i].unitno == that.ruleForm.unitno) {
              that.ruleForm.unitname = res.data[i].unitname;
            }
          }

          ElMessage.success({
            showClose: true,
            message: "查询成功",
            type: "success",
            center: true
          })
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    getOptions(){
      let that = this;
      get('/getaddunitnojs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.unitnoList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.unitnoList.push({value: res.data[i].unitno, label: res.data[i].unitno});
          }

          ElMessage.success({
            showClose: true,
            message: "查询成功",
            type: "success",
            center: true
          })
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    onSubmit(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          get('/addunitjs', {
            stepno: that.ruleForm.stepno,
            unitno: that.ruleForm.unitno,
            priority: that.ruleForm.priority
          }).then(function (res) {
            if (res.code === 1) {
              ElMessage.success({
                showClose: true,
                message: "新建成功",
                type: "success",
                center: true
              })
            } else {
              ElMessage.warning({
                showClose: true,
                message: "新建失败",
                type: "warning",
                center: true
              })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel(){
      let that = this;
      let router_target = "MainPage";
      that.$router.push({
        name: router_target
      })
    }
  }
}
</script>

<style scoped>

</style>


